package com.aiyige.page.my.merchandisemanagement.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.aiyige.page.my.merchandisemanagement.MMFilterable;
import com.aiyige.page.my.merchandisemanagement.mmlearnvideo.MMLearnVideoPage;
import com.aiyige.page.my.merchandisemanagement.mmmajorcourse.MMMajorCoursePage;
import com.aiyige.page.my.merchandisemanagement.mmpersonaltrainer.MMPersonalTrainerPage;
import com.aiyige.page.my.merchandisemanagement.mmtraingcard.MMTraingCardPage;
import com.aiyige.page.my.merchandisemanagement.mmtraingclass.MMTraingClassPage;
import com.aiyige.page.my.merchandisemanagement.mmuniversalproduct.MMUniversalProductPage;
import com.aiyige.page.my.merchandisemanagement.model.TabItem;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by zhuyakun on 2017/11/22.
 */

public class MMPagerAdapter extends FragmentPagerAdapter {

    List<TabItem> tabItemList;
    List<Fragment> fragmentList;

    public MMPagerAdapter(FragmentManager fm, List<TabItem> tabItemList) {
        super(fm);
        this.tabItemList = tabItemList;
        fragmentList = new LinkedList<>();
        for (TabItem tabItem : tabItemList) {
            switch (tabItem.getTabId()) {
                case TabItem.TAB_ID_LEARN_VIDEO:
                    fragmentList.add(MMLearnVideoPage.newInstance());
                    break;
                case TabItem.TAB_ID_MAJOR_COURSE:
                    fragmentList.add(MMMajorCoursePage.newInstance());
                    break;
                case TabItem.TAB_ID_TRAING_CLASS:
                    fragmentList.add(MMTraingClassPage.newInstance());
                    break;
                case TabItem.TAB_ID_PERSONAL_TRAINER:
                    fragmentList.add(MMPersonalTrainerPage.newInstance());
                    break;
                case TabItem.TAB_ID_TRAING_CARD:
                    fragmentList.add(MMTraingCardPage.newInstance());
                    break;
                case TabItem.TAB_ID_UNIVERSAL_PRODUCT:
                    fragmentList.add(MMUniversalProductPage.newInstance());
                    break;
            }
        }
    }

    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }

    @Override
    public int getCount() {
        return fragmentList.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabItemList.get(position).getTabName();
    }

    public void notifyFilterChanged(Integer filter) {
        for (Fragment fragment : fragmentList) {
            ((MMFilterable) fragment).setFilter(filter);
        }
    }
}
